package com.utaedang.jesseclub.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.util.Log;

public class JesseclubBoardHTMLParse
{
	private String url;
	private Context context;
	private Handler handler;
	private ProgressDialog progressDialog;
	private Source source;
	private ArrayList<HashMap<String, String>> data;
	
	public JesseclubBoardHTMLParse(Context context, Handler handler, ArrayList<HashMap<String, String>> data)
	{
		this.context = context;
		this.handler = handler;
		this.data = data;
	}
	
	public void open()
	{
		url = GlobalContext.JESSECLUB_URL;
		
		try
		{
			process();
		}catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	private void process() throws IOException
	{
		final Handler mHandler = new Handler();
		new Thread()
		{

			@Override
			public void run()
			{
				URL nURL;
				try
				{
					nURL = new URL(url);
					mHandler.post(new Runnable(){

						public void run()
						{
							progressDialog = ProgressDialog.show(context, "", "데이터 로딩 중...");
						}
					});
					
					data.clear();
					
					InputStream html = nURL.openStream();
					source = new Source(new InputStreamReader(html, "EUC-KR"));
					
					Element table = (Element) source.getAllElements(HTMLElementName.TABLE).get(5);
					
					Log.d("count", "테이블 수!!!!" + source.getAllElements(HTMLElementName.TABLE).size());
					
					int tr_count = table.getAllElements(HTMLElementName.TR).size();
					
					Log.d("count", "줄 수!!!!" + table.getAllElements(HTMLElementName.TR).size());
					
					Element tr = null;
					
					HashMap<String, String> hm = null;
					
					for(int i=1; i<tr_count; i++)
					{
						tr = (Element) table.getAllElements(HTMLElementName.TR).get(i);
						
						hm = new HashMap<String, String>();
						int td_count = tr.getAllElements(HTMLElementName.TD).size();

						try{
							if(td_count == 5){
								Log.d("count", "칸수!!!!" + td_count);
								Element td = null;
								String no = null;
								String href = null; 
								String txt = null;
								String writer = null;
								String subject = null;
								for(int j=0; j<td_count; j++){
									td = (Element)tr.getAllElements(HTMLElementName.TD).get(j);
									
									switch (j) {
									case 0:
										try{
										no = ((Element)td.getAllElements(HTMLElementName.FONT).get(0)).getContent().toString();
										}catch(Exception e){}
									case 1:
										try{
										Element a = td.getAllElements(HTMLElementName.A).get(0);
										
										href = a.getAttributeValue("href");
										txt = a.getContent().toString();
										}catch(Exception e){}
									case 2:
										try{
										writer = td.getContent().toString();
										}catch(Exception e){}
									case 4:
										try{
										subject = ((Element)td.getAllElements(HTMLElementName.FONT).get(0)).getContent().toString();
										}catch(Exception e){}
									}
								}
							
								if (txt.indexOf("추천주") > 0 || txt.indexOf("결산") > 0) {
									hm.put("no", no);
									hm.put("title", txt);
									hm.put("writer", writer);
									hm.put("date", subject);
									hm.put("url", href);

									data.add(hm);
								}
							}
						}catch(Exception e){}
					}
					
					mHandler.post(new Runnable()
					{
						public void run()
						{
							progressDialog.cancel();
							handler.sendEmptyMessage(0);
						}
					});
				}catch (MalformedURLException e)
				{
					progressDialog.cancel();
					handler.sendEmptyMessage(0);
				}
				catch (IOException e)
				{
					progressDialog.cancel();
					handler.sendEmptyMessage(0);
				}
				
			}
			
		}.start();
	}
}
